//
//  TestCollectionLayout.swift
//  ADKit_Example
//
//  Created by Adoma on 2024/12/5.
//  Copyright © 2024 CocoaPods. All rights reserved.
//

import UIKit
import ADKit

class TestCollectionLayoutController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    
    let page = ADHorizontalPageLayout(row: 2, column: 4)
    lazy var collection = UICollectionView(layout: page)
    
    let flow = ADHorizontalFlowLayout(row: 2, itemWidth: kScreenWidth/4)
    lazy var collection1 = UICollectionView(layout: flow)
    
    let flowPage = ADHorizontalFlowPageLayout(row: 2, column: 4)
    lazy var collection2 = UICollectionView(layout: flowPage)
    
    lazy var collection3 = UICollectionView { layout in
        layout.itemSize = CGSize(width: kScreenWidth/4, height: 75)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
    }
    
    var number = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        
        let titleLabel = UILabel(text: "ADHorizontalPageLayout", font: .pingfang(with: 12), color: ADKit.Color.k222222)
        view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(self.view.snp.topMargin)
            make.height.equalTo(20)
        }
        collection.delegate = self
        collection.dataSource = self
        collection.backgroundColor = .red
        collection.isPagingEnabled = true
        collection.register(CollectionCell.self)
        view.addSubview(collection)
        collection.snp.makeConstraints { make in
            make.width.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom)
            make.height.equalTo(150)
        }
        
        let titleLabel1 = UILabel(text: "ADHorizontalFlowLayout", font: .pingfang(with: 12), color: ADKit.Color.k222222)
        view.addSubview(titleLabel1)
        titleLabel1.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(collection.snp.bottom)
            make.height.equalTo(20)
        }
        collection1.delegate = self
        collection1.dataSource = self
        collection1.backgroundColor = .green
        collection1.register(CollectionCell.self)
        view.addSubview(collection1)
        collection1.snp.makeConstraints { make in
            make.width.equalToSuperview()
            make.top.equalTo(titleLabel1.snp.bottom)
            make.height.equalTo(150)
        }
        
        let titleLabel2 = UILabel(text: "ADHorizontalFlowPageLayout", font: .pingfang(with: 12), color: ADKit.Color.k222222)
        view.addSubview(titleLabel2)
        titleLabel2.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(collection1.snp.bottom)
            make.height.equalTo(20)
        }
        collection2.delegate = self
        collection2.dataSource = self
        collection2.backgroundColor = .blue
        collection2.isPagingEnabled = true
        collection2.register(CollectionCell.self)
        view.addSubview(collection2)
        collection2.snp.makeConstraints { make in
            make.width.equalToSuperview()
            make.top.equalTo(titleLabel2.snp.bottom)
            make.height.equalTo(150)
        }
        
        let titleLabel3 = UILabel(text: "UICollectionViewFlowLayout", font: .pingfang(with: 12), color: ADKit.Color.k222222)
        view.addSubview(titleLabel3)
        titleLabel3.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(collection2.snp.bottom)
            make.height.equalTo(20)
        }
        collection3.delegate = self
        collection3.dataSource = self
        collection3.backgroundColor = .yellow
        collection3.register(CollectionCell.self)
        view.addSubview(collection3)
        collection3.snp.makeConstraints { make in
            make.width.equalToSuperview()
            make.top.equalTo(titleLabel3.snp.bottom)
            make.height.equalTo(150)
        }
        
        let random = UIButton(type: .custom)
        random.addTarget(self, action: #selector(reloadAction), for: .touchUpInside)
        random.setTitle("Reload", for: .normal)
        random.setTitleColor(.black, for: .normal)
        view.addSubview(random)
        random.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(collection3.snp.bottom).offset(20)
        }
        
        reloadAction()
    }
    
    @objc func reloadAction() {
        let number = Array(1...24).random ?? 0
        self.title = "count \(number)"
        self.number = number
        
        let topBottomInset: CGFloat = [0,5,10].random ?? 0
        let leftRightInset: CGFloat = [0,5,10].random ?? 0
        
        let inset = UIEdgeInsets(top: topBottomInset,
                                 left: leftRightInset,
                                 bottom: topBottomInset,
                                 right: leftRightInset)
        
        collection.reloadData()
        
        if Bool.random() {
            flow.minimumColumn = 4
        } else {
            flow.minimumColumn = 0
        }
        collection1.reloadData()
        
        //        collection2.contentInset = inset
        collection2.reloadData()
        
        //        collection3.contentInset = inset
        collection3.reloadData()
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        self.number
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: CollectionCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
        cell.backgroundColor = .random
        cell.label.text = indexPath.item.toString
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        ADToast.show(msg: indexPath.item.toString)
    }
    
    class CollectionCell: UICollectionViewCell {
        let label = UILabel()
        override init(frame: CGRect) {
            super.init(frame: frame)
            label.textColor = ADKit.Color.k002FA7
            label.font = .systemFont(ofSize: 16)
            contentView.addSubview(label)
            label.snp.makeConstraints { make in
                make.center.equalToSuperview()
            }
        }
        
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
}
